package db;

import java.lang.reflect.Array;
import java.util.ArrayList;

import android.R.id;
import android.database.Cursor;

import common.Location;
import common.Promotion;
import common.PromotionImage;
import common.Store;
import common.StoreType;
import common.User;

public class DBHandle {
	
	
	/*USer*/
	public static void CreateTableUser(){
		DB db = new DB();
		db.CreateTableUser();
	}
	
	public static boolean AddDataLocation(){
		DB db = new DB();
		return db.AddDataLocation();
	}
	public static boolean UserAdd(User u){
		DB db = new DB();
		boolean re = db.UserAdd(u);
		return re;
	}
	public static User GetUser(){
		User u = new User();
		DB db = new DB();
		u = db.GetUser();		
		return u;
	}
	
	public static boolean UserUpdateLocation(String location_id_list){
		DB db = new DB();
		return db.UserUpdateLocation(location_id_list);		
		
	}
	public static boolean UserUpdateType(int type){
		DB db = new DB();
		return db.UserUpdateType(type);				
	}

	/*DN USER*/
	public static boolean CreateDBAndTable(){
		boolean result = false;		
		DB db = new DB();
		result = db.CreateDBAndTable("ar_mobile");
		return result;
	}
	
	public static ArrayList<Promotion> GetListPromotion(String sql){
		
		ArrayList<Promotion> lst = new ArrayList<Promotion>();
		DB db = new DB();
		lst =  db.GetListPromotion(sql);	
		return lst;		
		
	}
	
	public static ArrayList<Store> GetListStore(String sql){
		
		ArrayList<Store> lst = new ArrayList<Store>();
		DB db = new DB();
		lst  =  db.GetListStore(sql);
		return lst;	
		
		
	}

	public static boolean ReSetData(){
		DB db = new DB();
		return db.ReSetData("ar_mobile");
	}
	
	public static ArrayList<Location> GetListLocation(String sql){
		
		ArrayList<Location> lst = new ArrayList<Location>();
		DB db = new DB();
		lst =  db.GetListLocation(sql);
		return lst; 
	}
	
	public static ArrayList<PromotionImage> GetListPromotionImage(String sql){
		
		ArrayList<PromotionImage> lst = new ArrayList<PromotionImage>();
		DB db = new DB();
		lst =  db.GetListPromotionImage(sql);	
		return lst;	 
	}
	
	/*GET LIST DATA*/
	public static ArrayList<Location> GetListProvince(){
		String sql = "select id, name from ar_location where level = 3  order by name asc";
		ArrayList<Location> lst = new ArrayList<Location>();
		DB db = new DB();
		lst =  db.GetListLocation(sql);
		return lst;				
	}
	public static ArrayList<StoreType> GetListStoreType(){
		String sql ="select id,name from ar_store_type  order by name asc";
		ArrayList<StoreType> lst = new ArrayList<StoreType>();
		DB db = new DB();
		lst  =  db.GetListStoreType(sql);		
		return lst;	
	}
	/*END GET LIST DATA*/

	public static boolean AddPromotion(Promotion p){
		String sql ="insert into ar_promotion(id,name,old_price,percent,from_date,to_date,quantity,description,image)" +
				" values("+p.getId()+""
				+ ",'"+p.getName()+"'"
				+ ",'"+p.getOld_price()+"'"
				+ ",'"+p.getPercent()+"'"
				+ ",'"+p.getFrom_date()+"'"
				+ ",'"+p.getTo_date()+"'"
				+ ",'"+p.getQuantity()+"'"
				+ ",'"+p.getDescription()+"'"
				+ ",'"+p.getImage()+"');";
		
		DB db = new DB();
		return db.AddPromotion(sql);
		
	}
	
	public static boolean AddStore(Store s){
		
		String sql ="insert into ar_store(id,name,type,phone,mobile,lng,lat,alt,province,location_id,ward,street,house_number,vote,description,customer_id,chain_store_id)"
				+ " values("+s.getId()+"" 
				+ ",'"+s.getName()+"'"
				+ ","+s.getType()+""
				+ ",'"+s.getPhone()+"'"
				+ ",'"+s.getMobile()+"'"			
				+ ",'"+s.getLng()+"'"
				+ ",'"+s.getLat()+"'"
				+ ",'"+s.getAlt()+"'"
				+ ","+s.getProvince()+""
				+ ","+s.getLocation_id()+""
				+ ",'"+s.getWard()+"'"
				+ ",'"+s.getStreet()+"'"
				+ ",'"+s.getHouse_number()+"'"
				+ ","+s.getVote()+""
				+ ",'"+s.getDescription()+"'"
				+ ","+s.getCustomer_id()+""
				+ ","+s.getChain_store_id()+");";
		
		DB db = new DB();
		return db.AddPromotion(sql);
	}
	
	public static boolean Delete(String sql){
		DB db = new DB();
		return db.Delete(sql); 
	}
	
	/*begin promotion push*/
	
	public static boolean CreateTablePromotionPush(){		
		try{
			DB db = new DB();
			db.CreateTablePromotionPush();
			return true;
		}
		catch(Exception ex){
			
		}
		return false;
	}
	
	public static boolean AddPromotionPush(int promotion_id){		
		try{
			DB db = new DB();
			db.AddPromotionPush(promotion_id);
			return true;
		}
		catch(Exception ex){
			
		}
		return false;
	}
	
	public static ArrayList<Integer> GetListPtomotionPush(){
		try{
			ArrayList<Integer> lst = new ArrayList<Integer>();
			String sql = "select * from ar_promotion_push ";
			DB db = new DB();
			lst = db.GetListPtomotionPush(sql);
			int a = 5;
			a = 6;
			return lst;
		}catch(Exception ex){
			
		}
		return null;
	}
	
	public static String GetListPtomotionPushByString(){
		String id_list = "";
		try{
			ArrayList<Integer> lst = new ArrayList<Integer>();
			lst = GetListPtomotionPush();
			for(int i = 0 ; i < lst.size() ; i++){
				if(id_list.equals("")){
					id_list+= String.valueOf(lst.get(i));
				}else{
					id_list+= ","+String.valueOf(lst.get(i));
				}
			}
			return id_list;
		}catch(Exception ex){
			
		}
		return id_list;
	}
	
	
	/*end promotion push*/ 
	
	public static boolean DeletePromotionPush(String id_list){
		try{
			String sql =" delete from ar_promotion_push where promotion_id in ("+id_list+")";
			DB db = new DB();
			db.Delete(sql);
			return true;
		}catch(Exception ex){
			
		}
		return false;
	}
	
	/*BEGIN store type*/
	public static boolean AddListStoreType(ArrayList<StoreType> lst){
		if(lst.size() < 1){
			return false;
		}
		String sql = "";
		for(int i = 0 ; i < lst.size() ; i++){
			String id = String.valueOf(lst.get(i).getId());
			String name = String.valueOf(lst.get(i).getName());
			if(i == 0){
				sql+= "insert into ar_store_type(id,name) select "+id+" as id , '"+name+"' as name ";
			}else{
				sql += " union select "+id+" as id, '"+name+"' as name";
			}
		}
		try{
			if(!sql.equals("")){
				DB db = new DB();
				return db.AddListStoreType(sql);
			
			}
			return false;
		}catch(Exception ex){			
		}
		return false;
	}
	/*
	 String sql ="insert into ar_store_type(id,name)";	
			sql +="select 1 as id , 'Mua sắm' as name";
			sql += " union select 2 as id, 'Ăn uống' as name";
			sql += " union select 4 as id, 'Vui chơi, giải trí' as name";
			sql += " union select 8 as id, 'Công nghệ' as name";
			sql += " union select 16 as id, 'Du lịch' as name";
			*/
	
	
	/*End store type*/
	
	
}
